import MMUI.MMUIFactory
import fr.esir.imse.PollSystemStandaloneSetup
import fr.esir.imse.mapping.MappingDSLStandaloneSetup
import fr.esir.imse.mapping.mappingDSL.Mapping
import fr.esir.imse.pollSystem.PollSystem
import java.io.File
import java.io.IOException
import java.util.Collections
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import java.awt.Container
import org.eclipse.emf.common.util.EList
import fr.esir.imse.pollSystem.Poll
import java.util.Iterator
import fr.esir.imse.pollSystem.Question
import MMUI.Widget

class Transformateur {
	
	def void transformateur(String pollSystemDSL, String mapping) {



		// Create model
		var ui = MMUIFactory.eINSTANCE.createContainer

		// Create a resource set.
  		var ResourceSet resourceSet = new ResourceSetImpl();
  		
  		var map = testParsingMapping(mapping)
  		var poll = testParsingPollSystem(pollSystemDSL)
  		
  		
  		var EList<Poll> polls = poll.polls
  		var Iterator<Poll> pollsIterator = polls.iterator
  		
  		while(pollsIterator.hasNext){
  			var Poll p = pollsIterator.next
  			var EList<Question> questionsPoll = p.questions
  			var questionsPollIterator = questionsPoll.iterator
  			
  			while(questionsPollIterator.hasNext){
  				var questionPoll = questionsPollIterator.next as Question
  				var questionsMap = map.questions
  				var fr.esir.imse.mapping.mappingDSL.Question questionMap
  				
  				var questionsMapIterator = questionsMap.iterator
  				while(questionsMapIterator.hasNext && questionMap==null){
  					var element = questionsMapIterator.next
  					if(element.id.equals(questionPoll.id)) questionMap=element
  				}
  				
  				var Widget widget
  				if(questionMap.widget == 'radiobutton') widget= MMUIFactory.eINSTANCE.createRadio
  				else widget = MMUIFactory.eINSTANCE.createCheckbox
  				
  				widget.text=questionPoll.text
  				var option = questionPoll.options
				var optionIterator = option.iterator
				while(optionIterator.hasNext){
					var opt = MMUIFactory.eINSTANCE.createOption
					opt.text = optionIterator.next.text
					widget.options.add(opt)
				}
				ui.widget.add(widget)
  			}
  		}
  		
  		
	  	// Register the default resource factory -- only needed for stand-alone!
  		resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put(
    	Resource.Factory.Registry.DEFAULT_EXTENSION, new XMIResourceFactoryImpl());

		// Get the URI of the model file.
		var URI fileURI = URI.createFileURI(new File("myUI.xmi").getAbsolutePath());

		// Create a resource for this file.
  		var Resource resource = resourceSet.createResource(fileURI);
 
		// Add the book and writer objects to the contents.
	  	resource.getContents().add(ui);
	  	resource.getContents().add(MMUIFactory.eINSTANCE.createContainer);

		// Save the contents of the resource to the file system.
  		try
  		{
		    resource.save(Collections.EMPTY_MAP);
  		}
  		catch (IOException e) {}
	
		// Load model
		var loadedModel = resourceSet.getResource(fileURI, true)
		var Container loadedContainer = loadedModel.contents.head as Container
		
	}


	
	def PollSystem testParsingPollSystem(String pollSystem) {
		
		// Create a resource set.
  		var ResourceSet resourceSet = new ResourceSetImpl();
  		
  		PollSystemStandaloneSetup.doSetup
  		var resource = resourceSet.getResource(URI.createURI(pollSystem), true)
  		return resource.contents.head as PollSystem
	}
	
	def Mapping testParsingMapping(String mapping) {
		
		// Create a resource set.
  		var ResourceSet resourceSet = new ResourceSetImpl();
  		
  		MappingDSLStandaloneSetup.doSetup
  		var resource = resourceSet.getResource(URI.createURI(mapping), true)
  		return resource.contents.head as Mapping
	}

}
